package com.ruoyi.course.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.domain.Course;
import com.ruoyi.course.service.ICourseService;
import com.ruoyi.monitor.domain.Monitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课程列表Controller
 *
 * @author ruoyi
 * @date 2024-10-17
 */
@RestController
@RequestMapping("/course/course")
public class CourseController extends BaseController {
    @Autowired
    private ICourseService courseService;

    /**
     * 查询课程列表列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(Course course) {
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        return getDataTable(list);
    }

    /**
     * 导出课程列表列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:export')")
    @Log(title = "课程列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Course course) {
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        util.exportExcel(response, list, "课程列表数据");
    }

    /**
     * 获取课程列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(courseService.selectCourseById(id));
    }

    /**
     * 新增课程列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:add')")
    @Log(title = "课程列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Course course) {
        System.out.println(course);
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:edit')")
    @Log(title = "课程列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Course course) {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:remove')")
    @Log(title = "课程列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(courseService.deleteCourseByIds(ids));
    }

    /**
     * 导入文件
     */
    @PreAuthorize("@ss.hasPermi('course:course:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        List<Course> courseList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = courseService.importCourse(courseList, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载模版
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        util.importTemplateExcel(response, "课程数据");
    }

    /**
     * 获取家长孩子今日课程
     */
    @GetMapping("/getTodayCourse/{id}")
    public AjaxResult getTodayCourse(@PathVariable Long id) {
        return AjaxResult.success(courseService.getTodayCourse(id));
    }

    /**
     * 获取当前有效课程
     *
     * @param monitor
     * @return
     */
    @GetMapping("/getNowCourse")
    public AjaxResult getNowCourse(Monitor monitor) {
        return AjaxResult.success(courseService.getNowCourse(monitor));
    }
}
